#include "test_os.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "stdlib.h"
#include "cm_fs.h"
#include "stdio.h"
#include "limits.h"

static osThreadId_t cm_test_thread = NULL;

/******************wdt******************/
static osThreadId_t cm_test_wdt_thread = NULL;
static void cm_test_thread_wdt_task_func(void *argument)
{
    cm_demo_printf("wdt argument:%s\n",argument);
    // int ThreadGetState = -1;    
    // int ThreadGetState1 = -1;
    // unsigned int TickCount = 0;
    // int test_fd = cm_fs_open("thread_schedule_tick.txt", CM_FS_AB);
    // char buf[66] = {0};
    int times = 8000000;
    while (times--) {
        // ThreadGetState = osThreadGetState(cm_test_thread);
        // if (ThreadGetState1 != ThreadGetState) {
        //     ThreadGetState1 = ThreadGetState;
        //     TickCount = osKernelGetTickCount();
        //     sprintf(buf, "%d_%u%s",ThreadGetState, TickCount, "\n");
        //     int32_t fs_write = cm_fs_write(test_fd, buf, 10);
        // }
        //osDelay(1);
    }
    cm_demo_printf("osThreadExit1\n");
    //cm_fs_close(test_fd);
}
/******************thread******************/

static uint32_t flags = 0;
static void cm_test_thread_task_func(void *argument)
{
    int times = 10;
    while(times)
    {
        osDelay(800);
        cm_demo_printf("argument:%s\n",argument);
        // flags = osThreadFlagsGet ();//ml307a 不支持
        cm_demo_printf("osThreadFlagsGet ret:%u\n",flags);
        // if(1 == flags)
        // {
        //     cm_demo_printf("exit thread!\n");
        //     cm_demo_printf("osThreadFlagsClear ret:%u\n",osThreadFlagsClear(flags));//ml307a 不支持
        //     osThreadExit();
        // }
        // cm_demo_printf("osThreadFlagsWait ret:%u\n", osThreadFlagsWait(2, osFlagsWaitAny, 200));//ml307a 不支持
        cm_demo_printf("get thread name:%s\n",osThreadGetName (cm_test_thread));
        cm_demo_printf("get thread id:%u\n", osThreadGetId ());
        cm_demo_printf("thread stack size:%d\n", osThreadGetStackSize (cm_test_thread));
        cm_demo_printf("thread stack remain size:%d\n", osThreadGetStackSpace (cm_test_thread));
        cm_demo_printf("test thread state:%d\n",osThreadGetState (cm_test_thread));
        times--;
    }
    //cm_demo_printf("osThreadTerminate ret:%d\n", osThreadTerminate(cm_test_thread));
    cm_demo_printf("osThreadExit\n");
    cm_test_thread = NULL;
    //osThreadExit();
}

/******************timer******************/
static osTimerId_t os_test_timer;
static int times = 0;
static void cm_test_timer_func (void *argument)
{
    times++;
    //cm_demo_printf("timer argument:%s\n",argument);
    //cm_demo_printf("os osTimerGetName: %s\n",osTimerGetName (os_test_timer));
    
}

/******************event******************/
static osEventFlagsId_t test_event_flags_id;

/******************mutex******************/
static int test_mutex = 0;
static osMutexId_t test_nutex_id = 0;
static osThreadId_t cm_test_mutex_thread = NULL;
static void cm_test_mutex_thread_task(void *argument)
{
    cm_demo_printf("%s\n",argument);
    while(1)
    {
        osStatus_t ret = osMutexAcquire (test_nutex_id, 10);
        cm_demo_printf("os osMutexAcquire ret:%d\n",ret);
        if(0 > ret)
        {
            cm_demo_printf("os osMutexAcquire fail\n");
        }
        else{
            test_mutex++;
            cm_demo_printf("test_mutex:%d\n",test_mutex);
            cm_demo_printf("os osMutexGetOwner ret(thread id):%p\n",  osMutexGetOwner (test_nutex_id));
            cm_demo_printf("os osMutexRelease ret:%d\n", osMutexRelease (test_nutex_id));             //释放
        }
        osDelay(100);
    }
}
/******************semaphore******************/
static osThreadId_t cm_test_semaphore_thread = NULL;
static osSemaphoreId_t test_semaphore_id = 0;
static void cm_test_semaphore_thread_task(void *argument)
{
    while(1)
    {
        int32_t ret = osSemaphoreAcquire (test_semaphore_id, 100);
        cm_demo_printf("os osSemaphoreAcquire ret:%d\n", ret);
        if(0 > ret)
        {
            cm_demo_printf("os osSemaphoreAcquire fail!\n");
        }
        else
        {
            cm_demo_printf("os osSemaphoreRelease ret:%d\n", osSemaphoreRelease(test_semaphore_id));
        }
        osDelay(100);
    }
}

/******************message queue******************/
static osMessageQueueId_t cm_test_messagequeue_id = NULL;
static osThreadId_t cm_test_msgqueue_thread = NULL;
static void cm_test_msgqueue_thread_task(void *argument)
{
    cm_demo_printf("argument:%s\n",argument);
    //int times = 10;
    //while(times)
    while(1)
    {
        //const void *msg_ptr = malloc(50);
        char msg_ptr[60]={0};
        uint8_t msg_prio = 0;
        int32_t ret =osMessageQueueGet (cm_test_messagequeue_id, msg_ptr, &msg_prio, 20);
        cm_demo_printf("os osMessageQueueGet ret:%d\n", ret);
        cm_demo_printf("msg_ptr:%s, msg_prio:%u\n",msg_ptr,msg_prio);
        if(0 > ret)
        {
            cm_demo_printf("os osMessageQueueGet fail!\n");
        }
        else
        {
            cm_demo_printf("os osMessageQueuePut ret:%d\n", osMessageQueuePut (cm_test_messagequeue_id, "thread put", (uint8_t)10, 20));
        }
        //free(msg_ptr);
        osDelay(400);
        //times--;
    }
}

/******************MEMORY POOL******************/
static osMemoryPoolId_t cm_test_mempool_id = NULL;

 

 

void cm_test_os_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("KERNEL",cmd[2],strlen("KERNEL")))
    {
        if(0 == strncmp("INIT",cmd[3],strlen("INIT")))//ml307a 不支持
        {
            //cm_demo_printf("osKernelInitialize ret:%d\n",osKernelInitialize ());//ml307a 不支持
        }
        else if(0 == strncmp("GET_INFO",cmd[3],strlen("GET_INFO")))//ml307a 不支持
        {
            // osVersion_t version = {0};
            // char id_buf[50] = {0};
            // uint32_t id_size = atoi(cmd[4]);
            // cm_demo_printf("osKernelGetInfo ret:%d\n", osKernelGetInfo (&version,id_buf,id_size));
            // cm_demo_printf("version:api %d, kernel %d; kernel id:%s\n",version.api,version.kernel,id_buf);
        }
        else if(0 == strncmp("STATE",cmd[3],strlen("STATE")))//ml307a 不支持
        {
            // cm_demo_printf("osKernelGetState ret:%d\n",osKernelGetState ());
        }
        else if(0 == strncmp("START",cmd[3],strlen("START")))//ml307a 不支持
        {
            // cm_demo_printf("osKernelStart ret:%d\n", osKernelStart ());
        }
        else if(0 == strncmp("LOCK",cmd[3],strlen("LOCK")))//ml307a 不支持
        {
            int32_t KernelLock = osKernelLock ();
            int i = 0;
            i++;
            i++;
            i++;
            // cm_demo_printf("osKernelLock ret:%d\n",KernelLock);
            // OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,KernelLock,osKernelLock);
            int32_t KernelunLock = osKernelUnlock ();
            cm_demo_printf("osKernelLock ret:%d\n",KernelLock);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,KernelLock,osKernelLock);
            cm_demo_printf("osKernelUnlock ret:%d\n",KernelunLock);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,KernelunLock,osKernelUnlock);
        }
        else if(0 == strncmp("UNLOCK",cmd[3],strlen("UNLOCK")))//ml307a 不支持
        {            
            int32_t KernelunLock = osKernelUnlock ();
            cm_demo_printf("osKernelUnlock ret:%d\n",KernelunLock);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,KernelunLock,osKernelUnlock);
            
        }
        else if(0 == strncmp("RESTORELOCK",cmd[3],strlen("RESTORELOCK")))//ml307a 不支持
        {
            // int32_t lock = atoi(cmd[4]);
            // cm_demo_printf("osKernelRestoreLock ret:%d\n",osKernelRestoreLock(lock));
        }
        else if(0 == strncmp("SUSPEND",cmd[3],strlen("SUSPEND")))//ml307a 不支持
        {
            // cm_demo_printf("osKernelSuspend ret:%d\n",osKernelSuspend ());
        }
        else if(0 == strncmp("RESUME",cmd[3],strlen("RESUME")))//ml307a 不支持
        {
            // uint32_t sleep_ticks = atoi(cmd[4]);
            // osKernelResume (sleep_ticks);
        }
        else{
            cm_demo_printf("param error!\n");
        }
    }
    else if(0 == strncmp("TICK",cmd[2],strlen("TICK")))
    {
        cm_demo_printf("kernel tick count:%d\n", osKernelGetTickCount());
        uint32_t KernelGetTickFreq = osKernelGetTickFreq ();
        cm_demo_printf("kernel tick freq:%d\n",KernelGetTickFreq);
        OCTEST_FUNC_EXPECT_EQUAL_UINT32(200,KernelGetTickFreq,osKernelGetTickFreq);
    }
    // else if(0 == strncmp("TIMER",cmd[2],strlen("TIMER")))//ml307a 不支持
    // {
    //     // cm_demo_printf("osKernelGetSysTimerCount ret:%d\n",  osKernelGetSysTimerCount ());
    //     // cm_demo_printf("osKernelGetSysTimerFreq ret:%d\n",  osKernelGetSysTimerFreq ());
    // }
    else if(0 == strncmp("THREAD",cmd[2],strlen("THREAD")))
    {
        if(0 == strcmp("CREAT",cmd[3]))
        {
                const osThreadAttr_t attr = {
                "cm-os_test_thread_name",   /**< name of the thread. */
                0,   /**< attribute bits. */
                NULL,    /**< memory for control block. */
                NULL,   /**< size of provided memory for control block. */
                NULL,    /**< memory for stack. */
                2048,   /**< size of stack. */
                osPriorityBelowNormal,   /**< initial thread priority (default: osPriorityNormal). */
                0,   /**< TrustZone module identifier. */
                0,   /**< reserved (must be 0). */
            };
            char *argument = "first argu";
            cm_test_thread = osThreadNew(cm_test_thread_task_func, argument, &attr);
            OCTEST_FUNC_EXPECT_NOT_NULL(cm_test_thread, osThreadNew);
            if (NULL == cm_test_thread)
            {
                cm_demo_printf("creat thread fail\n");
                return;
            }
        }else if(0 == strcmp("TERMINATE",cmd[3]))
        {
            int ThreadTerminate = osThreadTerminate(cm_test_thread);
            cm_demo_printf("osThreadTerminate ret:%d\n",ThreadTerminate);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ThreadTerminate,osThreadTerminate);
        }else if(0 == strcmp("STATE",cmd[3]))
        {
            int ThreadGetState = osThreadGetState(cm_test_thread);
            cm_demo_printf("test thread state:%d\n", ThreadGetState);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0, ThreadGetState, osThreadGetState);
        }
        else if(0 == strcmp("STACK_SIZE", cmd[3]))
        {
            uint32_t ThreadGetStackSize = osThreadGetStackSize (cm_test_thread);
            cm_demo_printf("osThreadGetStackSize ret:%d\n",ThreadGetStackSize);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,ThreadGetStackSize,osThreadGetStackSize);
        }
        else if(0 == strcmp("SET_GET_PRIORITY",cmd[3]))
        {
            osPriority_t priority = (osPriority_t)atoi(cmd[4]);
            cm_demo_printf("osThreadSetPriority ret:%d\n", osThreadSetPriority (cm_test_thread, priority));
            cm_demo_printf("get priority:%d\n", osThreadGetPriority (cm_test_thread));
        }
        else if(0 == strcmp("SUSPEND",cmd[3]))
        {
            cm_demo_printf("osThreadSuspend ret:%d\n",  osThreadSuspend (cm_test_thread));
        }
        else if(0 == strcmp("RESUME",cmd[3]))
        {
            cm_demo_printf("osThreadResume ret:%d\n", osThreadResume (cm_test_thread));
        }else if(0 == strcmp("DETACH",cmd[3]))
        {
            //cm_demo_printf("osThreadDetach ret:%d\n",osThreadDetach (cm_test_thread));//ml307a不支持
        }else if(0 == strcmp("JOIN",cmd[3]))
        {
            //cm_demo_printf("osThreadJoin ret:%d\n", osThreadJoin (cm_test_thread));//ml307a不支持
        }
        else if(0 == strcmp("SET_FLAG",cmd[3]))
        {
            uint32_t flags = atoi(cmd[4]);
            // cm_demo_printf("osThreadFlagsSet ret:%u\n", osThreadFlagsSet (cm_test_thread,  flags));//ml307a 不支持
        }
        else if(0 == strcmp("YIELD",cmd[3]))
        {
            cm_demo_printf("osThreadYield ret:%d\n", osThreadYield());
        }
        else if(0 == strcmp("GET_COUNT",cmd[3]))
        {
            uint32_t os_count = osThreadGetCount ();
            cm_demo_printf("osThreadGetCount ret:%d\n",os_count);
            // osThreadId_t *thread_array = (osThreadId_t*)malloc(os_count*sizeof(osThreadId_t));
            // if(NULL == thread_array)
            // {
            //     cm_demo_printf("malloc fail!\n");
            //     free(thread_array);
            //     return;
            // }
            // cm_demo_printf("osThreadEnumerate ret:%d\n",osThreadEnumerate(thread_array, os_count)); //ml307a 不支持
            // for(int i = 0;i<os_count;++i)
            // {
            //     cm_demo_printf("the %d thread: %p,%u\n",i,thread_array+i,*(thread_array+i));
            // }
            // free(thread_array);
        }
        // else if(0 == strcmp("EXIT",cmd[3]))
        // {
        //     osThreadExit();
        // }
        else
        {
            cm_demo_printf("param error!\n");
        }
    }else if(0 == strncmp("DELAY",cmd[2],strlen("DELAY")))
    {
        // uint32_t tick = atoi(cmd[3]);
        // uint32_t kernel_tick = osKernelGetTickCount ();
        // cm_demo_printf("tick before osDelay:%u\n",kernel_tick);
        cm_demo_printf("tick before osDelay\n");
        cm_demo_printf("osDelay ret:%d\n", osDelay (atoi(cmd[3])));
        cm_demo_printf("tick after osDelay\n");
        // cm_demo_printf("tick after osDelay:%u\n",osKernelGetTickCount ());

        // cm_demo_printf("tick after osDelayUntil:%u\n", osDelayUntil (kernel_tick + tick + tick));
    }else if(0 == strncmp("TIMER",cmd[2],strlen("TIMER")))
    {
        if(0 == strcmp(cmd[3],"NEW"))
        {
            const osTimerAttr_t attr = {
            "test_timer",   /**< name of the timer. */
            0,   /**< attribute bits. */
            0,    /**< memory for control block. */
            0,   /**< size of provided memory for control block. */
        };
        void *argument = "os test timer";
        os_test_timer = osTimerNew(cm_test_timer_func,osTimerPeriodic,argument,&attr);
        cm_demo_printf("os osTimerNew ret:%p\n",os_test_timer);
        }
        else if(0 == strcmp(cmd[3],"START"))
        {
            uint32_t ticks = atoi(cmd[4]);
            cm_demo_printf("osTimerStart ret:%d\n", osTimerStart(os_test_timer,ticks));
        }else if(0 == strcmp(cmd[3],"GET_NAME"))
        {
            cm_demo_printf("os osTimerGetName: %s\n",osTimerGetName (os_test_timer));
        }
        else if(0 == strcmp(cmd[3],"STOP"))
        {
            cm_demo_printf("os osTimerStop ret:%d\n",osTimerStop (os_test_timer));
        }
        else if(0 == strcmp(cmd[3],"STATE"))
        {
            cm_demo_printf("os osTimerIsRunning ret:%d, times:%d\n",osTimerIsRunning (os_test_timer),times);
        }
        else if(0 == strcmp(cmd[3],"DELETE"))
        {
            cm_demo_printf("os osTimerDelete ret:%d\n", osTimerDelete (os_test_timer));
        }
        else
        {
            cm_demo_printf("param error!\n");
        }

    }else if(0 == strncmp("EVENT",cmd[2],strlen("EVENT")))
    {
        if(0 == strcmp(cmd[3],"NEW"))
        {
            const osEventFlagsAttr_t attr = {
                "test_eflag",   /**< name of the event flags. */
                0,   /**< attribute bits. */
                0,    /**< memory for control block. */
                0,   /**< size of provided memory for control block. */
            };
            test_event_flags_id = osEventFlagsNew (&attr);
            cm_demo_printf("os osEventFlagsNew ret:%u\n",test_event_flags_id);
        }
        else if(0 == strcmp(cmd[3],"WAIT"))
        {
            char *p;
            uint32_t flags = strtoul(cmd[4],&p,10);
            uint32_t timeout = strtoul(cmd[5],&p,16);
            uint32_t ret = osEventFlagsWait (test_event_flags_id,flags, osFlagsWaitAny, timeout);
            cm_demo_printf("os osEventFlagsWait ret:%u\n",ret);
        }else if(0 == strcmp(cmd[3],"SET"))
        {
            char *p;
            uint32_t flags = strtoul(cmd[4],&p,10);
            cm_demo_printf("os osEventFlagsSet ret(flags):%u\n", osEventFlagsSet (test_event_flags_id, flags));
        }else if(0 == strcmp(cmd[3],"GET"))
        {
            cm_demo_printf("os osEventFlagsGet ret(flags):%u\n", osEventFlagsGet (test_event_flags_id));
        }else if(0 == strcmp(cmd[3],"CLEAR"))
        {
            char *p;
            uint32_t flags = strtoul(cmd[4],&p,10);
            cm_demo_printf("os osEventFlagsClear ret(flags):%u\n", osEventFlagsClear (test_event_flags_id, flags));
        }else if(0 == strcmp(cmd[3],"DELETE"))
        {
            cm_demo_printf("os osEventFlagsDelete ret:%u\n", osEventFlagsDelete (test_event_flags_id));
        }
        else
        {
            cm_demo_printf("param error!\n");
        }
    }else if(0 == strncmp("MUTEX",cmd[2],strlen("MUTEX")))
    {
        if(0 == strcmp("NEW",cmd[3]))
        {
            osMutexAttr_t attr = {
                "test_mutex",   /**< name of the mutex. */
                0,   /**< attribute bits. */
                0,    /**< memory for control block. */
                0,   /**< size of provided memory for control block. */
            };
            test_nutex_id = osMutexNew (&attr);
            cm_demo_printf("os osMutexNew ret:%u\n",test_nutex_id);

            const osThreadAttr_t thread_attr = {
                    "cm-os_test_thread_name",   /**< name of the thread. */
                    0,   /**< attribute bits. */
                    NULL,    /**< memory for control block. */
                    NULL,   /**< size of provided memory for control block. */
                    NULL,    /**< memory for stack. */
                    2048,   /**< size of stack. */
                    osPriorityBelowNormal,   /**< initial thread priority (default: osPriorityNormal). */
                    0,   /**< TrustZone module identifier. */
                    0,   /**< reserved (must be 0). */
                };
                char *argument = "mutex thread";
                cm_test_mutex_thread = osThreadNew (cm_test_mutex_thread_task, argument, &thread_attr); 
        }
        else if (0 == strcmp("ACQUIRE",cmd[3]))
        {
            cm_demo_printf("os osMutexAcquire ret:%d\n", osMutexAcquire (test_nutex_id, 10));
            test_mutex++;
            cm_demo_printf("test_mutex: %d\n",test_mutex);
        }else if(0 == strcmp("RELEASE",cmd[3]))
        {
            cm_demo_printf("os osMutexRelease ret:%d\n", osMutexRelease (test_nutex_id));
        }else if(0 == strcmp("OWNER",cmd[3]))
        {
            cm_demo_printf("os osMutexGetOwner ret(thread id):%p\n",  osMutexGetOwner (test_nutex_id));
        }else if(0 == strcmp("DELETE",cmd[3]))
        {
            cm_demo_printf("os osMutexDelete ret:%d\n", osMutexDelete (test_nutex_id));
            cm_demo_printf("os osThreadTerminate ret:%d\n", osThreadTerminate(cm_test_mutex_thread));
        }else
        {
            cm_demo_printf("param error!\n");
        }
    }else if(0 == strncmp("SEMAPHORE",cmd[2],strlen("SEMAPHORE")))
    {
        if(0 == strcmp("NEW",cmd[3]))
        {
            const osThreadAttr_t thread_attr = {
            "cm-os_test_thread_name",   /**< name of the thread. */
            0,   /**< attribute bits. */
            NULL,    /**< memory for control block. */
            NULL,   /**< size of provided memory for control block. */
            NULL,    /**< memory for stack. */
            2048,   /**< size of stack. */
            osPriorityBelowNormal,   /**< initial thread priority (default: osPriorityNormal). */
            0,   /**< TrustZone module identifier. */
            0,   /**< reserved (must be 0). */
            };
            char *argument = "semaphore thread";
            cm_test_semaphore_thread = osThreadNew (cm_test_semaphore_thread_task, argument, &thread_attr); 
            
            uint32_t max_count = strtoul(cmd[4],(void**)0,10);
            uint32_t initial_count = strtoul(cmd[5],(void**)0,10);
            const osSemaphoreAttr_t attr = {
                "test_semaphore",   /**< name of the semaphore. */
                0,  /**< attribute bits. */
                0,    /**< memory for control block. */
                0,   /**< size of provided memory for control block. */
            };
            test_semaphore_id = osSemaphoreNew (max_count, initial_count, &attr);
            cm_demo_printf("os osSemaphoreNew ret(id):%u\n",test_semaphore_id);
        }else if(0 == strcmp("ACQUIRE",cmd[3]))
        {
            uint32_t ret = osSemaphoreAcquire (test_semaphore_id, 100);
            cm_demo_printf("os osSemaphoreAcquire ret:%d\n", ret);
        }
        else if(0 == strcmp("RELEASE",cmd[3]))
        {
            cm_demo_printf("os osSemaphoreRelease ret:%d\n", osSemaphoreRelease(test_semaphore_id));
        }
        else if(0 == strcmp("GETCOUNT",cmd[3]))
        {
            cm_demo_printf("os osSemaphoreGetCount ret:%d\n", osSemaphoreGetCount (test_semaphore_id));
        }
        else if(0 == strcmp("DELETE",cmd[3]))
        {
            cm_demo_printf("os osThreadTerminate ret:%d\n", osThreadTerminate(cm_test_semaphore_thread));
            cm_demo_printf("os osSemaphoreDelete ret:%d\n",  osSemaphoreDelete (test_semaphore_id));
        }
        else{
            cm_demo_printf("param error!\n");
        }

    }else if(0 == strncmp("MEMPOOL",cmd[2],strlen("MEMPOOL")))//ml307a 不支持
    {
        // if(0 == strcmp("NEW",cmd[3]))
        // {
        //     uint32_t block_count = atoi(cmd[4]);
        //     uint32_t block_size = atoi(cmd[4]);
        //     osMemoryPoolAttr_t attr = {0};
        //     cm_test_mempool_id = osMemoryPoolNew (block_count, block_size,&attr);
        //     cm_demo_printf("mempool id:%p\n",cm_test_mempool_id);
        // }
        // else if(0 == strcmp("ALLOC",cmd[3]))
        // {
        //     uint32_t timeout = atoi(cmd[4]);
        //     void *alloc_addr = osMemoryPoolAlloc (cm_test_mempool_id, timeout);
        //     cm_demo_printf("alloc_addr:%p\n",alloc_addr);
        // }
        // else if(0 == strcmp("FREE",cmd[3]))
        // {
        //     void *block = strtoul(cmd[4],(void**)0,16);
        //     cm_demo_printf("osMemoryPoolFree ret:%d\n",osMemoryPoolFree (cm_test_mempool_id,block));
        // }
        // else if(0 == strcmp("GET_CAPACITY",cmd[3]))
        // {
        //     cm_demo_printf("osMemoryPoolGetCapacity ret:%d\n",osMemoryPoolGetCapacity (cm_test_mempool_id));
        // }
        // else if(0 == strcmp("GET_BLOCK_SIZE",cmd[3]))
        // {
        //     cm_demo_printf("osMemoryPoolGetBlockSize ret:%d\n",osMemoryPoolGetBlockSize (cm_test_mempool_id));
        // }
        // else if(0 == strcmp("GET_COUNT",cmd[3]))
        // {
        //     cm_demo_printf("osMemoryPoolGetCount ret:%d\n",osMemoryPoolGetCount (cm_test_mempool_id));
        // }
        // else if(0 == strcmp("GET_SPACE",cmd[3]))
        // {
        //     cm_demo_printf("osMemoryPoolGetSpace ret:%d\n",osMemoryPoolGetSpace (cm_test_mempool_id));
        // }
        // else if(0 == strcmp("DELETE",cmd[3]))
        // {
        //     cm_demo_printf("osMemoryPoolDelete ret:%d\n",osMemoryPoolDelete (cm_test_mempool_id));
        // }
        // else
        // {
        //     cm_demo_printf("param error!\n");
        // }
    }  
    else if(0 == strncmp("MESSAGE_QUEUE",cmd[2],strlen("MESSAGE_QUEUE")))
    {
        if(0 == strcmp("NEW",cmd[3]))
        {
            osMessageQueueAttr_t attr = {
            "test_messagequeue",   /**< name of the message queue. */
                1,   /**< attribute bits. */
                0,    /**< memory for control block. */
                0,   /**< size of provided memory for control block. */
                0,    /**< memory for data storage. */
                0,   /**< size of provided memory for data storage. */
            };
            uint32_t msg_count = strtoul(cmd[4],(void**)0,10);
            uint32_t msg_size = strtoul(cmd[5],(void**)0,10);
            cm_test_messagequeue_id = osMessageQueueNew ( msg_count, msg_size, &attr);
            cm_demo_printf("os osMessageQueueNew ret(id):%u\n",cm_test_messagequeue_id);

            const osThreadAttr_t thread_attr = {
                    "cm-os_test_thread_name",   /**< name of the thread. */
                    0,   /**< attribute bits. */
                    NULL,    /**< memory for control block. */
                    NULL,   /**< size of provided memory for control block. */
                    NULL,    /**< memory for stack. */
                    2048,   /**< size of stack. */
                    osPriorityBelowNormal,   /**< initial thread priority (default: osPriorityNormal). */
                    0,   /**< TrustZone module identifier. */
                    0,   /**< reserved (must be 0). */
                };
                char *argument = "msgqueue thread";
                cm_test_msgqueue_thread = osThreadNew (cm_test_msgqueue_thread_task, argument, &thread_attr); 
        }else if (0 == strcmp("QUEUEPUT",cmd[3]))
        {
            const void *msg_ptr = cmd[4];
            uint8_t msg_prio = (uint8_t)strtoul(cmd[5],(void**)0,10);
            cm_demo_printf("msg_prio      %hu\n",msg_prio);
            uint32_t timeout = strtoul(cmd[6],(void**)0,10);
            cm_demo_printf("os osMessageQueuePut ret:%d\n", osMessageQueuePut (cm_test_messagequeue_id, msg_ptr, msg_prio, timeout));

        }else if(0 == strcmp("QUEUEGET",cmd[3]))
        {
            //const void *msg_ptr = malloc(50);
            char msg_ptr[60]={0};
            uint8_t msg_prio = 0;
            uint32_t timeout = strtoul(cmd[4],(void**)0,10);
            cm_demo_printf("os osMessageQueueGet ret:%d\n",  osMessageQueueGet (cm_test_messagequeue_id, msg_ptr, &msg_prio, timeout));
            cm_demo_printf("msg_ptr:%s, msg_prio:%hu\n",msg_ptr,msg_prio);
            //free(msg_ptr);
        }else if(0 == strcmp("CAPACITY",cmd[3]))
        {
            cm_demo_printf("os osMessageQueueGetCapacity ret:%u\n",osMessageQueueGetCapacity (cm_test_messagequeue_id));
        }else if(0 == strcmp("MSGSIZE",cmd[3]))
        {
            cm_demo_printf("os osMessageQueueGetMsgSize ret:%u\n", osMessageQueueGetMsgSize (cm_test_messagequeue_id));
        }else if(0 == strcmp("COUNT",cmd[3]))
        {
            cm_demo_printf("os osMessageQueueGetCount ret:%u\n",  osMessageQueueGetCount (cm_test_messagequeue_id));
        }else if(0 == strcmp("SPACE",cmd[3]))
        {
            cm_demo_printf("os osMessageQueueGetSpace ret:%u\n",  osMessageQueueGetSpace (cm_test_messagequeue_id));
        }else if(0 == strcmp("RESET",cmd[3]))
        {    
            cm_demo_printf("os osMessageQueueReset ret:%d\n",  osMessageQueueReset (cm_test_messagequeue_id));
        }else if(0 == strcmp("DELETE",cmd[3]))
        {
            cm_demo_printf("os osThreadTerminate ret:%d\n", osThreadTerminate(cm_test_msgqueue_thread));
            cm_demo_printf("os osMessageQueueReset ret:%d\n",  osMessageQueueDelete (cm_test_messagequeue_id));
        }else
        {
            cm_demo_printf("param error!\n");
        }
    }
    else if(0 == strncmp("WDT",cmd[2],strlen("WDT")))
    {
        if (NULL == cm_test_wdt_thread)
        {
            const osThreadAttr_t attr = {
                "cm-os_test_thread_name",   /**< name of the thread. */
                0,   /**< attribute bits. */
                NULL,    /**< memory for control block. */
                NULL,   /**< size of provided memory for control block. */
                NULL,    /**< memory for stack. */
                2048,   /**< size of stack. */
                atoi(cmd[3]),   /**< initial thread priority (default: osPriorityNormal). */
                0,   /**< TrustZone module identifier. */
                0,   /**< reserved (must be 0). */
            };
            char *argument = "wdt first argu";
            cm_test_wdt_thread = osThreadNew (cm_test_thread_wdt_task_func, argument, &attr); 
            OCTEST_FUNC_EXPECT_NOT_NULL(cm_test_wdt_thread,osThreadNew);
            if (NULL == cm_test_wdt_thread)
            {
                cm_demo_printf("creat thread fail\n");
                return;
            }
        }
    }
    else{
        cm_demo_printf("param error!\n");
    }
}